// pages/topicality/xiaofeiquan/xiaofeiquan.js
//获取应用实例
const app = getApp()
const util = require("../../../utils/util.js");
var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
Page({

    /**
     * 页面的初始数据
     */
    data: {
        citycode: "sz",
        city: '深圳',
        province: '广东',
        pindex: 0,
        cindex: 0,
        cityPick: ["选择城市"],
        showNav: true, // 快捷导航是否展开
        showMore: false,
        showtk: false,
        showShare: true, // 显示分享按钮
        inputValue: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        util.getUrl(app.globalData.url).then((res) => {
            this.getProvinceData(options.city);
        })
    },

    // 获取定位
    getLocation() {
        qqmapsdk.reverseGeocoder({
            success: (res) => {
                let city = res.result.address_component.city;
                // let city="芜湖市";
                let citydata = this.data.citylist.filter(v => {
                    if (city && city.indexOf(v.cityname) != -1) {
                        return v;
                    }
                });

                if (citydata != '' && city.indexOf(this.data.city) == -1) {
                    ks.showModal({
                        content: '您当前定位城市为' + city + '\n是否需要切换到' + city + '核酸检测查询？',
                        success: res => {
                            if (res.confirm) {
                                this.setData({
                                    city: Array.isArray(citydata) ? citydata[0].cityname : this.data.city,
                                    citycode: Array.isArray(citydata) ? citydata[0].citycode : this.data.citycode,
                                    province: Array.isArray(citydata) ? citydata[0].province : this.data.province,
                                    cityPick: ["选择城市"]
                                })
                                let clist = [];
                                this.data.provPick.forEach((v, k) => {
                                    if (v == this.data.province) {
                                        this.setData({
                                            pindex: k,
                                        })
                                    }
                                })
                                this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                                    clist.push(v.cityname)
                                })
                                this.setData({
                                    cityPick: this.data.cityPick.concat(clist)
                                })
                                this.data.cityPick.forEach((v, k) => {
                                    if (v == this.data.city) {
                                        this.setData({
                                            cindex: k,
                                        })
                                    }
                                })
                                // 获取城市公众号二维码
                                this.getQrcode(this.data.citycode);
                                this.getInfoData(this.data.citycode);
                            }
                        }
                    })
                }

            },
        });
    },

    // 获取城市公众号二维码
    getQrcode(citycode) {
        util.cityConfig(citycode).then(res => {
            this.setData({
                qrcodeimg: res.qrcode ? res.qrcode : '',
            })
        })
    },

    // 获取有隔离政策的城市
    getProvinceData(optcode) {
        let data = {
            action: "hesuan",
            type: "city",
        };
        util.request('zhuanti.php', data, ).then(res => {
            this.setData({
                cityArray: res.data.data.city,
            })
            let provPick = [];
            let citylist = [];
            let clist = [];
            this.data.cityArray.forEach(v => {
                v.citylist.forEach(v => {
                    citylist.push(v)
                })
                provPick.push(v.province)
                this.setData({
                    provPick,
                    citylist
                })
            })
            if (this.data.citylist) {
                this.data.citylist.forEach((v, k) => {
                    if (optcode == v.citycode) {
                        this.setData({
                            citycode: v.citycode,
                            city: v.cityname,
                            province: v.province,
                        })
                    } else {
                        this.setData({
                            citycode: this.data.citycode,
                            city: this.data.city,
                            province: this.data.province,
                        })
                    }
                })
            }

            this.data.provPick.forEach((v, k) => {
                if (v == this.data.province) {
                    this.setData({
                        pindex: k,
                    })
                }
            })

            this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                clist.push(v.cityname)
            })
            this.setData({
                cityPick: this.data.cityPick.concat(clist)
            })

            this.data.cityPick.forEach((v, k) => {
                if (v == this.data.city) {
                    this.setData({
                        cindex: k,
                    })
                }
            })
            // 获取城市公众号二维码
            this.getQrcode(this.data.citycode);
            this.getInfoData();
            if (this.data.citylist != "" && Array.isArray(this.data.citylist)) {
                // 获取定位
                this.getLocation();
            }
        })
    },

    // 核酸检测信息
    getInfoData(citycode) {
        let data = {
            action: "hesuan",
            citycode: citycode ? citycode : this.data.citycode
            // citycode: 'bj',
        };
        util.request('zhuanti.php', data, ).then(res => {
            this.selectComponent("#fangyiTools").getTools(citycode)
            if (res.data.data) {
                ks.setNavigationBarTitle({
                    title: res.data.data ? res.data.data.title : '核酸检测'
                })
                console.log(res.data.data.daofen)
                if (res.data.data.daofen) {
                    res.data.data.daofen.content = res.data.data.daofen.content
                        .replace(/<p([\s\w"=\/\.:;]+)((?:(="[^"]+")))/ig, '<p')
                        .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
                        .replace(/<p([\s\w"=\/\.:;]+)((?:(class="[^"]+")))/ig, '<p')
                        .replace(/<p>/ig, '<p class="p_class">')
                        .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
                        .replace(/<img/gi, '<img style="width:100%;height:auto"')
                }
                let resData = res.data.data.other;
                let tempArr = [];
                let dataInfo = [];
                for (let i = 0; i < resData.length; i++) {
                    if (tempArr.indexOf(resData[i].block) === -1) {
                        dataInfo.push({
                            block: resData[i].block,
                            showMore1: true,
                            child: [resData[i]]
                        });
                        tempArr.push(resData[i].block);
                    } else {
                        for (let j = 0; j < dataInfo.length; j++) {
                            if (dataInfo[j].block == resData[i].block) {
                                dataInfo[j].child.push(resData[i]);
                                break;
                            }
                        }
                    }
                }
                let temparr = {};
                let count = 0;
                dataInfo.forEach((v, k) => {
                    count++;
                    if (v.block == "核酸检测地点") {
                        temparr.place = v;
                    } else if (v.block == "核酸检测费用") {
                        temparr.price = v;
                    } else if (v.block == "核酸检测条件") {
                        temparr.tiaojian = v;
                    } else if (v.block == "核酸检测最新消息") {
                        temparr.msg = v;
                    }
                    if (count == dataInfo.length) {
                        this.setData({
                            infoData: temparr
                        })
                    }

                })

                if (res.data.data.yuyue) {
                    if (res.data.data.yuyue.length > 1) {
                        this.setData({
                            showMore: true,
                        })
                    }
                }
                this.setData({
                    hsData: res.data.data,
                })
            }
        })
    },

    // 省份选择
    bindProv: function (e) {
        this.setData({
            pindex: e.detail.value,
            cindex: 0,
            cityPick: ["选择城市"],
        })
        let clist = [];
        this.data.cityArray[this.data.pindex].citylist.forEach(v => {
            clist.push(v.cityname)
        })
        this.setData({
            cityPick: this.data.cityPick.concat(clist),
        })
    },

    // 城市选择
    bindCity(e) {
        this.setData({
            cindex: e.detail.value
        })
        let cityname = this.data.cityPick[this.data.cindex];
        if (this.data.citylist) {
            this.data.citylist.forEach(v => {
                if (v.cityname == cityname) {
                    let citycode = v.citycode;
                    this.setData({
                        citycode,
                        city: cityname
                    })
                }
            })
        }
        // 获取城市公众号二维码
        this.getQrcode(this.data.citycode);
        this.getInfoData(this.data.citycode);
    },

    // 输入框赋值
    input(e) {
        this.setData({
            inputValue: e.detail.value
        })
    },
    // 点击搜索
    tojigou() {
        const content =this.data.inputValue.trim()
        if (content) {
            ks.navigateTo({
                url: `/pages/topicality/jiancejigou/jiancejigou?city=${this.data.city}&content=${content}`,
              })
        }else{
            ks.showToast({
              title: '不能为空哦~',
              icon: 'none'
            })
        }
      
    },
    // 跳转内容页
    navDetail(e) {
        let id = e.currentTarget.dataset.id;
        let type = e.currentTarget.dataset.type;
        if (id != 0) {
            ks.navigateTo({
                url: '/pages/article/detail/detail?id=' + id + '&type=' + type + '&city=' + this.data.citycode,
            })
        }
    },

    // 预约查看更多
    seemore() {
        this.setData({
            showMore: false,
        })
    },
    // 其他查看更多
    seemoreBtn(e) {
        let name = e.currentTarget.dataset.name;
        for (let key in this.data.infoData) {
            if (this.data.infoData[key].block == name) {
                this.data.infoData[key].showMore1 = false;
            }
        }
        this.setData({
            infoData: this.data.infoData
        })
    },

    yuyueBtn(e) {
        let id = e.currentTarget.dataset.id;
        let type = e.currentTarget.dataset.type;
        let url = e.currentTarget.dataset.url;
        let title = e.currentTarget.dataset.title;
        if (id != 0) {
            ks.navigateTo({
                url: '/pages/article/detail/detail?id=' + id + '&type=' + type + '&city=' + this.data.citycode,
            })
        } else {
            this.setData({
                showtk: true
            })
            util.navContact(title, url)
        }
    },

    // 进入客服会话
    contact() {
        this.setData({
            showtk: false,
        })
    },

    // 关闭
    close() {
        this.setData({
            showtk: false,
        })
    },


    /**
     * 页面滑动事件的处理函数
     */
    onPageScroll: function (e) {
        this.setData({
            showNav: false,
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: this.data.hsData ? this.data.hsData.title : "核酸检测查询",

        }
    },

    // 分享朋友圈
    onShareTimeline: function () {
        return {
            title: this.data.hsData ? this.data.hsData.title : "核酸检测查询",
            imageUrl: "http://imgbdb3.bendibao.com/xcx/20205/20/2020520135146_96009.png",
            query: {
                city: this.data.citycode
            },
        }
    },
})